package com.yycwxt.modules.admin.cgj_xscb_gdpdk.cgj;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yycwxt.common.BaseService;
import com.yycwxt.modules.admin.data_import.AccountData;
import com.yycwxt.modules.admin.data_import.ImpFile;
import com.yycwxt.modules.admin.data_import.ImpFileMapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Mr.Zhang on 2022/9/15 22:04
 */
@Service
@Transactional
public class ImpCgjService extends BaseService<ImpFileMapper, ImpFile> {

    private final ImpCgjMapper impCgjMapper;

    public ImpCgjService(ImpCgjMapper impCgjMapper) {
        this.impCgjMapper = impCgjMapper;
    }

    public List<ImpFile> selectImpFiles(int zq) {
        return impCgjMapper.selectImpFiles(zq);
    }

    @SneakyThrows
    @Transactional
    public void impCgj(MultipartFile file) {
        EasyExcel.read(file.getInputStream(), Cgj.class, new ReadListener<Cgj>() {

            private static final int BATCH_SIZE = 1000;

            @Override
            public void invoke(Cgj data, AnalysisContext context) {

                System.out.println(data);
                // 执行存储过程
                impCgjMapper.impCgj(
                        data.getHpmc(),
                        data.getSjbm(),
                        data.getCgj1(),
                        data.getCgj2(),
                        data.getTjrq(),
                        AccountData.getAccountDate()
                );
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {

            }
        }).sheet().doRead();
    }
}
